Longest Substring with K Distinct Characters (medium)

Problem Statement #

Given a string, find the length of the longest substring in it with no more than K distinct characters.

Example 1:

Input: String="araaci", K=2
Output: 4
Explanation: The longest substring with no more than '2' distinct characters is "araa".

Example 2:

Input: String="araaci", K=1
Output: 2
Explanation: The longest substring with no more than '1' distinct characters is "aa".

Example 3:

Input: String="cbbebi", K=3
Output: 5
Explanation: The longest substrings with no more than '3' distinct characters are "cbbeb" & "bbebi".

Try it yourself #

Try solving this question here:

0 of 3 Tests Passed
ResultInputExpected OutputActual OutputReason
findLength(araaci, 2)4-1Incorrect Output
findLength(araaci, 1)2-1Incorrect Output
findLength(cbbebi, 3)5-1Incorrect Output
7.186s

Solution #

This problem follows the Sliding Window pattern and we can use a similar dynamic sliding window strategy as discussed in Smallest Subarray with a given sum. We can use a HashMap to remember the frequency of each character we have processed. Here is how we will solve this problem:

  1. First, we will insert characters from the beginning of the string until we have ‘K’ distinct characters in the HashMap.
  2. These characters will constitute our sliding window. We are asked to find the longest such window having no more than ‘K’ distinct characters. We will remember the length of this window as the longest window so far.
  3. After this, we will keep adding one character in the sliding window (i.e. slide the window ahead), in a stepwise fashion.
  4. In each step, we will try to shrink the window from the beginning if the count of distinct characters in the HashMap is larger than ‘K’. We will shrink the window until we have no more than ‘K’ distinct characters in the HashMap. This is needed as we intend to find the longest window.
  5. While shrinking, we’ll decrement the frequency of the character going out of the window and remove it from the HashMap if its frequency becomes zero.
  6. At the end of each step, we’ll check if the current window length is the longest so far, and if so, remember its length.

Here is the visual representation of this algorithm for the Example-1:

Created with Fabric.js 1.6.0-rc.1 window end window start window start window end window start window end window start window end window start window end window start window end window start window end window start window end window start window end a r a a c i K = 2 Max Length = 0 a r a a c i a r a a c i Max Length = 1 Max Length = 2 a r a a c i Max Length = 3 a r a a c i Max Length = 4 a r a a c i a r a a c i Max Length = 4 Max Length = 4 a r a a c i a r a a c i Max Length = 4 Max Length = 4 window start window end a r a a c i Number of distinct characters > 2, let's shrink the sliding window Number of distinct characters are still > 2, let's shrink the sliding window Number of distinct character > 2, let's shrink the sliding window

Code #

Here is how our algorithm will look:

Output

0.730s

Length of the longest substring: 4 Length of the longest substring: 2 Length of the longest substring: 5

Time Complexity #

The time complexity of the above algorithm will be O(N)O(N) where ‘N’ is the number of characters in the input string. The outer for loop runs for all characters and the inner while loop processes each character only once, therefore the time complexity of the algorithm will be O(N+N)O(N+N) which is asymptotically equivalent to O(N)O(N).

Space Complexity #

The space complexity of the algorithm is O(K)O(K), as we will be storing a maximum of ‘K+1’ characters in the HashMap.

Mark as Completed
←    Back
Smallest Subarray with a given sum (easy)
Next    →
Fruits into Baskets (medium)